<template>
  <div id="M-router">
    <div class="block">
      <div>我的关注</div>
      <el-tree
        :data="data"
        node-key="id"
        default-expand-all
        :expand-on-click-node="false"
      >
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span>{{ node.label }}</span>
          <span>
            <el-button v-if="data.id == 0" type="text" size="mini"
              ><el-button type="text" @click="() => append(data)"
                >添加</el-button
              ></el-button
            >

            <el-button
              type="text"
              size="mini"
              v-if="data.id != 0"
              @click="() => remove(node, data)"
            >
              删除
            </el-button>
          </span>
        </span>
      </el-tree>
    </div>
  </div>
</template>


<script scope>
// import MymusicTree from "./MymusicChiden/MymusicTree.vue";
// import Songsheet from './MymusicChiden/Songsheet.vue';
let id = 1000;

export default {
  data() {
    const data = [
      {
        id: 0,
        label: "我的歌单列表 ",
        children: [
          {
            id: 1,
            label: "我的收藏",
          },
        ],
      },
    ];
    return {
      data: JSON.parse(JSON.stringify(data)),
    };
  },

  methods: {
    // 添加
    append(data) {
      this.$prompt("请输入歌单名", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
      // 回调
        .then(({ value }) => {
          // 歌单添加
          const newChild = { id: id++, label: value };
          if (!data.children) {
            this.$set(data, "children", []);
          }
          data.children.push(newChild);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消添加",
          });
        });
    },
  // 删除歌单
    remove(node, data) {
      const parent = node.parent;
      const children = parent.data.children || parent.data;
      const index = children.findIndex((d) => d.id === data.id);
      children.splice(index, 1);
    },
  },
};
</script>
<style scoped>
</style>
